package com.neudu.yiyang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.yiyang.commons.JSONReturn;
import com.neudu.yiyang.mapper.NursecontentMapper;
import com.neudu.yiyang.pojo.Nursecontent;
import com.neudu.yiyang.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/nursingContent")
public class NurseContentController {

    @Autowired
    private NursecontentMapper nursecontentMapper;
    @Autowired
    private JSONReturn jsonReturn;

    // 查询所有护理内容（带分页和条件查询）
    @PostMapping("/get_all_nursingcontent")
    public String getAll(@RequestBody Map<String, Object> params) {
        try {
            int pageNum = params.get("pageNum") == null ? 1 : (int) params.get("pageNum");
            int pageSize = params.get("pageSize") == null ? 10 : (int) params.get("pageSize");
            String nursingName = (String) params.get("nursingName");
            PageHelper.startPage(pageNum, pageSize);

            LambdaQueryWrapper<Nursecontent> queryWrapper = new LambdaQueryWrapper<>();
            if (StringUtils.hasText(nursingName)) {
                queryWrapper.like(Nursecontent::getNursingName, nursingName);
            }

            List<Nursecontent> list = nursecontentMapper.selectList(queryWrapper);
            PageInfo<Nursecontent> pageInfo = new PageInfo<>(list);

            Map<String, Object> result = new HashMap<>();
            result.put("list", pageInfo.getList());
            result.put("total", pageInfo.getTotal());

            return jsonReturn.returnSuccess(result);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError("查询失败: " + e.getMessage());
        }
    }

    // 新增一条护理内容
    @PostMapping("/add_one_nursingContent")
    public String addOne(@RequestBody Nursecontent nursecontent) {
        try {
            // 设置默认值
            if (nursecontent.getStatus() == null) {
                nursecontent.setStatus(1); // 默认启用
            }
            if (nursecontent.getIsDeleted() == null) {
                nursecontent.setIsDeleted(0); // 默认未删除
            }
            if (nursecontent.getExecutionCycle() == null) {
                nursecontent.setExecutionCycle(""); // 默认空字符串
            }
            if (nursecontent.getExecutionTimes() == null) {
                nursecontent.setExecutionTimes(""); // 默认空字符串
            }
            if (nursecontent.getSerialNumber() == null) {
                // 生成一个简单的序列号，实际项目中可以用更复杂的逻辑
                nursecontent.setSerialNumber("NC" + System.currentTimeMillis() % 10000);
            }

            int result = nursecontentMapper.insert(nursecontent);
            return result > 0 ? jsonReturn.returnSuccess("添加成功") : jsonReturn.returnError("添加失败");
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError("添加失败: " + e.getMessage());
        }
    }
    // 修改一条护理内容
    @PutMapping("/modify_nursingContent_by_id")
    public String modifyById(@RequestBody Nursecontent nursecontent) {
        try {
            if (nursecontent.getId() == null) {
                return jsonReturn.returnError("ID不能为空");
            }

            int result = nursecontentMapper.updateById(nursecontent);
            return result > 0 ? jsonReturn.returnSuccess() : jsonReturn.returnError("更新失败");
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError("更新失败: " + e.getMessage());
        }
    }

    // 删除一条护理内容
    @DeleteMapping("/remove_nursingContent_by_id")
    public String removeById(@RequestParam("id") Integer id) {
        try {
            if (id == null) {
                return jsonReturn.returnError("ID不能为空");
            }

            int result = nursecontentMapper.deleteById(id);
            return result > 0 ? jsonReturn.returnSuccess() : jsonReturn.returnError("删除失败");
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError("删除失败: " + e.getMessage());
        }
    }

    // 登录接口
    @PostMapping("/user/login")
    public String login(@RequestBody User user) {
        try {
            // 这里应该有实际的登录逻辑，例如验证用户名和密码
            // 模拟登录成功
            if ("admin".equals(user.getUsername()) && "123456".equals(user.getPassword())) {
                Map<String, Object> data = new HashMap<>();
                data.put("token", "mock-token-" + System.currentTimeMillis());
                return jsonReturn.returnSuccess(data);
            } else {
                return jsonReturn.returnError("用户名或密码错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError("登录失败: " + e.getMessage());
        }
    }
}